/**
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author naveen and clint
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TicTacToe extends JFrame implements ActionListener{
    

    /**
     * constant for Tie
     */
    public static final int TIE = 0;

    /**
     * constant for player 1 winning
     */
    public static final int PLAYER1_WIN = 1;
    
    /**
     * constant for player 2 winning
     */
    public static final int PLAYER2_WIN = 2;

    /**
     * constant for no winner yet
     */
    public static final int NO_WINNER_YET = 3;

    /**
     * maintains the JButtons for the playing field
     *  should be in a 3 X 3 array
     */
    private JButton [][] buttonField; 
   
    /**
     * keeps track of which players turn it is
     */
    private JButton reset;
    private int turn; 
 
    /**
     * keeps track of how many button clicks have occured
     */
    private int click;
    

    /**
     * Label which displays which players turn it is and the winner
     */
    private JLabel infoLabel;
   



    public TicTacToe(){
	super("Tic Tac Toe");
    	this.setSize(400,400);
	
	//initialize turn and click attributes
    	turn=1;
    	click=0;

	//this calls the private helper method setup
	this.setup();
	       
	//set the window default closing operation here
	super.setDefaultCloseOperation(EXIT_ON_CLOSE);

	//make the frame visible		
       this.setVisible(true);
    }
    
    private void setup(){
	Container contentPane = this.getContentPane();
	contentPane.setLayout(new GridLayout(0,3));
	// change the background color
	contentPane.setBackground(new Color(50,200,100));
	contentPane.setForeground(new Color(0, 0, 0));

	buttonField=new JButton[3][3];
	
	
	// add remaining buttons here and set their bounds
	
	for(int i=0; i<=2; i++){
		for(int j=0; j<=2; j++){
			this.buttonField[i][j]=new JButton();
			this.buttonField[i][j].setBounds(100,20,80,80);
			this.buttonField[i][j].setActionCommand(i+","+j);
			contentPane.add(this.buttonField[i][j]);
			this.buttonField[i][j].addActionListener(this);
			
		}
		
	}
	
	

	
	this.infoLabel = new JLabel();
	//the label initially displays this
        this.infoLabel.setText("Turn: Player1(O)");
	this.infoLabel.setBounds(10,310,200,30);
	contentPane.add(infoLabel);	
	
	this.reset=new JButton();
	this.reset.setActionCommand("Reset");
	this.reset.setText("Reset");
	Color theColor=new Color(0);
	this.reset.setBackground(theColor.lightGray);
	this.reset.addActionListener(this);
	contentPane.add(this.reset);
    }

    // Complete the implementation of actionPerformed()
    public void actionPerformed(ActionEvent ae) {
    	String source = ae.getActionCommand();
    	if(source.equals("Reset")){
    		this.setVisible(false);
    		TicTacToe theGame=new TicTacToe();
    		
    		
    	}else{
    	String text; //"O" for player 1 and "X" for player 2
    	int xValue=0;
    	int yValue=0;
    	
    	String[] yx=ae.getActionCommand().split(",");
    	yValue=Integer.parseInt(yx[0]);
    	xValue=Integer.parseInt(yx[1]);
    	
    	
    	click++; // incrementing the number of clicks
    	
	//it was player 1's turn so
    	if(turn == 1){
	    //whose turn is next
    		turn=2;
    		infoLabel.setText("Turn: Player2(X)");
	    //what should the text be on the
	    // button
    		text="O";
    		

    	}
    	else{
	   //whose turn is next
    		turn=1;
	    //what should the text be on the
	    // button
    		text="X";
    		infoLabel.setText("Turn: Player1(O)");
    	}

    	if(true){
	    // if first button is clicked setText to "O"
    	
    	    this.buttonField[yValue][xValue].setText(text); 
    	}
    	//write the actions for the remaining buttons here

    	}

	/******************************************************************************
	 * getWinner() returns 0 if it is a tie, returns 1 if player 1 is winner,     *
	 * 2 if player 2 is winner and 3 if a winner cannot be determined at this time*
	 * Based on the returned value set the text on label infoLabel appropriately  *  
	 * Hint: it returns one of the Constant values above                          *
	 ******************************************************************************/
       	int winner =getWinner();
       	if(winner==PLAYER1_WIN){
       		infoLabel.setText("Player 1 wins");
       		
       		for(int i=0; i<=2; i++){
       			for(int j=0; j<=2; j++){
       				this.buttonField[i][j].removeActionListener(this);
       			}
       		}
       	}
       	else if(winner==PLAYER2_WIN){
       		infoLabel.setText("Player 2 wins");
       		
       		for(int i=0; i<=2; i++){
       			for(int j=0; j<=2; j++){
       				this.buttonField[i][j].removeActionListener(this);       		
       			}       			
       		}
       	}
       	else if(winner==TIE){
       		infoLabel.setText("Tie Game");
       		
       		for(int i=0; i<=2; i++){
       			for(int j=0; j<=2; j++){		
       				this.buttonField[i][j].removeActionListener(this);

       			}
       		}
   		
       	}
    	
    }

    /**
     * This method determine if a player has won the game or not
     * Hint: just hard code the possible winning combinations here
     *   and return the appropriate value: tie = 0, player1 has won = 1
     *   player 2 has won = 2, or no winner yet = 3 
     * Hint: what would the case be for a Tie?
     *  USE CONSTANTS!!!!
     */
    public int getWinner(){
    	int result=NO_WINNER_YET;
    
        for(int i=0; i<=2; i++){
        	int counter=0;
        
        	for(int j=0; j<=2; j++){
       		
       		if(buttonField[i][j].getText().equals("O")){
       			counter++;
       			
       			if(counter==3){
       				
       				return PLAYER1_WIN;
       				
       			}
     
       		}
       		if(buttonField[i][j].getText().equals("X")){
       			counter--;
       			if(counter==-3){
       				return PLAYER2_WIN;
       			}
     
       		}
       				
       		
       	}
        }
        for(int j=0; j<=2; j++){
        	int counter=0;
        
        	for(int i=0; i<=2; i++){
       		
       		if(buttonField[i][j].getText().equals("O")){
       			counter++;
       			if(counter==3){
       			
       				return PLAYER1_WIN;
       				
       			}
     
       		}
       		if(buttonField[i][j].getText().equals("X")){
       			counter--;
       			if(counter==-3){
       				return PLAYER2_WIN;
       			}
       			
     
       		}
       		
       				
       		
       	}
        }
        if(buttonField[0][0].getText().equals("O")&&buttonField[1][1].getText().equals("O")&&buttonField[2][2].getText().equals("O")){
        	result= PLAYER1_WIN;
        }
       
        if(buttonField[0][0].getText().equals("X")&&buttonField[1][1].getText().equals("X")&&buttonField[2][2].getText().equals("X")){
        	result= PLAYER2_WIN;
        }
        if(buttonField[0][2].getText().equals("O")&&buttonField[1][1].getText().equals("O")&&buttonField[2][0].getText().equals("O")){
        	result=PLAYER1_WIN;
        }
       
        if(buttonField[0][2].getText().equals("X")&&buttonField[1][1].getText().equals("X")&&buttonField[2][0].getText().equals("X")){
        	result=PLAYER2_WIN;
        }
        if(click==9){
        	result= TIE;
        }

	return result;
    }


    public static void main(String[] args) {
	TicTacToe theGame = new TicTacToe();
    }
}
